﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ViewMaker.Core.Controls;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Windows;
using System.Windows.Media;
using System.Dynamic;
using System.Windows.Input;
using System.ComponentModel.DataAnnotations;
using System.Windows.Markup;
using ViewMaker.Core.ViewModels;

namespace ViewMaker.Core.Controls
{
    [View(ViewControlType.Grid, Title="Window Settings")]
    [ViewProperty(ViewControl.Properties.Width, 400)]
    public class WindowSettings : ViewModelBase, IEditableObject
    {
        public int? Width { get; set; }
        public int? Height { get; set; }

        public double? FontSize { get; set; }

        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "ColorList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string Foreground { get; set; }

        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "ColorList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string Background { get; set; }

        [Browsable(false)]
        [XmlIgnore]
        public string[] ColorList
        {
            get { return typeof(Colors).GetProperties().Select(x => x.Name).ToArray(); }
        }

#if !SILVERLIGHT
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "ResizeModeList")]
        public ResizeMode? ResizeMode { get; set; }

        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "StateList")]
        public WindowState? State { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        public IEnumerable<ResizeMode> ResizeModeList { get { return Enum.GetValues(typeof(ResizeMode)).Cast<ResizeMode>(); } }

        [XmlIgnore]
        [Browsable(false)]
        public IEnumerable<WindowState> StateList { get { return Enum.GetValues(typeof(WindowState)).Cast<WindowState>(); } }
#endif

        public string ClosingCommand { get; set; }

        [Browsable(false)]
        public string Template { get; set; }

        [XmlIgnore]
        [Display(Name = "", Description = "Tempalte for Xaml Generator")]
        [View(ViewControlType.Hyperlink)]
        [ViewProperty(HyperlinkViewControl.Properties.Command, "EditTemplateCommand")]
        public string EditTemplateLink { get { return "XamlGenerator Template"; } }


        [XmlIgnore]
        [Browsable(false)]
        public ICommand EditTemplateCommand { get { return CreateCommand(EditTemplate); } }
        private void EditTemplate()
        {
            ViewUtil.ShowDialog(new TemplateEditViewModel(this));
        }


        #region ShouldSerialize

        public bool ShouldSerializeWidth() { return Width.HasValue; }
        public bool ShouldSerializeHeight() { return Height.HasValue; }
        public bool ShouldSerializeFontSize() { return FontSize.HasValue; }
#if !SILVERLIGHT
        public bool ShouldSerializeResizeMode() { return ResizeMode.HasValue; }
        public bool ShouldSerializeState() { return State.HasValue; }
#endif
        #endregion

        public WindowSettings()
        {
        }

        #region IEditableObject メンバー

        private WindowSettings original;

        void IEditableObject.BeginEdit()
        {
            original = (WindowSettings)this.MemberwiseClone();
        }

        void IEditableObject.CancelEdit()
        {
            foreach (var prop in typeof(WindowSettings).GetProperties().Where(x=>x.CanWrite))
            {
                prop.SetValue(this, prop.GetValue(original, null), null);
            }
        }

        void IEditableObject.EndEdit()
        {
        }

        #endregion
    }
}
